local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local game_Cfg=_G.CommonOHOH.config.Game

--概述：控制怪物的动画播放

----------
local gHurtScaleSpeed =game_Cfg.monster_hurt_speed_boost --受伤怪物移动减速

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.Animatior=self.parent:AddComponent(_G.CommonOHOH.load.CharacterAnimationCtrl()); 

  
    self.Animatior:PreLoadHadAnimation(nil,nil,{["attack"]=false,["run"]=true,["idle"]=true,["damage"]=false,["die"]=false}); 


    self.Animatior:PlayAnimation("idle")
    self.AnimatiorState="idle"

    --追击中，播放行走动画
    self.parent.ChaseTargetEvent:Connect(function()
        self.Animatior:StopAll()
        self.Animatior:PlayAnimation("run")
        self.AnimatiorState="run"
    end)
  
    --受伤动画播放完毕，恢复之前的状态
    self.parent.HurtEvent:Connect(function(a,b,isDead)
        if isDead then
            return 
        end
        self.Animatior:StopAll()
        if  self.RestoreStateCon then
            self.RestoreStateCon:Disconnect()
            self.RestoreStateCon=nil
        end
       self.parent:ScaleMove(gHurtScaleSpeed)
       local useTime= self.Animatior:PlayAnimationByTime("damage",0.5)
       self.RestoreStateCon= self:AddProcess(function()
            self:Wait(useTime)
            self.parent:OpenMove()
            self:RestoreState()
        end)

    end)

    -- 死亡动画，播放完毕，直接销毁
    self.parent.DieEvent:Connect(function()
        self.Animatior:StopAll()

        if  self.RestoreStateCon then
            self.RestoreStateCon:Disconnect()
            self.RestoreStateCon=nil
        end

        self.parent:CloseMove()
        -- local dt=self.Animatior:PlayAnimationByTime("die",0.5)
        local dt=self.Animatior:PlayAnimation("die")

        if not dt then
            return 0.1
        end
         return dt-0.1
    end)

    --监听攻击时间
    self.parent.AttackEvent:Connect(function()
        self:Attack()
    end)

    self.parent.PauseEvent:Connect(function()
        self.Animatior:StopAll()
    end)

    self.parent.ContinueEvent:Connect(function()
        self.Animatior:PlayAnimation("idle")
        self.AnimatiorState="idle"
    end)
end;


-- 播放吐便便动画
function M.PlayPoo(self)
    self:Attack()
end

function M.Attack(self)
    self.Animatior:StopAll()
    self.parent:CloseMove()
    if  self.RestoreStateCon then
        self.RestoreStateCon:Disconnect()
        self.RestoreStateCon=nil
    end
   local useTime= self.Animatior:PlayAnimation("attack","attack")
   self.RestoreStateCon= self:AddProcess(function()
        self:Wait(useTime)
        self.parent:OpenMove()
        self:RestoreState()
    end)
end

function M.RestoreState(self)
    self.Animatior:StopAll()
    if self.AnimatiorState then
        self.Animatior:PlayAnimation(self.AnimatiorState)
    end
end


function M.dtor(self)
    if  self.RestoreStateCon then
        self.RestoreStateCon:Disconnect()
        self.RestoreStateCon=nil
    end
    self.Animatior:dtor()
	M.super.dtor(self);
end



return M